package 剑指offer;

public class Offer20_JZ53 {
    static int i = 0;
    public static boolean isNumeric (String str) {
        int len = str.length();
        if (len == 0) {
            return false;
        }

        boolean numeric = scanInteger(str);
        if (i<len && str.charAt(i)=='.') {
            ++i;
            if (i == len) {return numeric;}
            numeric = scanUnsignedInteger(str) || numeric;
        }
        if (i<len && (str.charAt(i)=='e' || str.charAt(i)=='E')) {
            i++;
            if (i == len) {return false;}
            numeric = numeric && scanInteger(str);
        }
        return numeric && i==len;
    }

    public static boolean scanInteger(String str) {
        if (str.charAt(i)=='+' || str.charAt(i)=='-') {
            ++i;
        }
        return scanUnsignedInteger(str);
    }

    public static boolean scanUnsignedInteger(String str) {
        int before = i;
        while (i<str.length() && str.charAt(i)>='0' && str.charAt(i)<='9') {
            ++i;
        }
        return i>before;
    }

    public static void main(String[] args) {
        //System.out.println(isNumeric("+100"));
//        System.out.println(isNumeric("123.45e+6"));
//        System.out.println(isNumeric("3.1416"));
        System.out.println(isNumeric("12e"));
    }
}
